package com.reader.cms.controller.content;

import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.content.ReaderPictureContent;
import com.reader.cms.service.content.IReaderPictureContentService;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import com.reader.core.entity.ResultMessageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;

import org.springframework.transaction.annotation.Transactional;

/**
 * @description   功能描述
 * @author        gumenghao
 * @date          2018-11-02
 */
@RestController
@RequestMapping("/api/readerpicturecontent")
public class ReaderPictureContentController extends BaseController {
private final Logger logger = LoggerFactory.getLogger(ReaderPictureContentController.class);

@Autowired
public IReaderPictureContentService iReaderPictureContentService;


/**
 * @function 分页查询数据
 *
 * @param currentPage   当前页
 * @param size   每页记录数
 * @param param   其它查询条件，根据情况动态添加
 * @return
 */
@GetMapping("list")
public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage ,
                                                @RequestParam(value = "size") int size,
                                                @RequestParam(value = "state",required = false) String state,
                                                @RequestParam(value = "rangeDate[]",required = false) String[] rangeDate) {
       Page<ReaderPictureContent> list=iReaderPictureContentService.selectListPage(currentPage,size,state,rangeDate);
       return ResultMessageInfo.ofSuccess("查询成功！", list);
}

/**
 * @function 添加新记录
 * @param readerPictureContent 实体对象
 * @return
 */
@PostMapping("insert")
public ResultMessageInfo insert(@RequestBody ReaderPictureContent readerPictureContent) {
    ValidatorUtil.validateEntity(readerPictureContent, AddGroup.class);//非空验证

    Integer userId = this.getUserId();
    readerPictureContent.setInsertDt(new Date());
    readerPictureContent.setDelFlag(0);
    readerPictureContent.setInsertUserId(userId);
    readerPictureContent.setUpdtUserId(userId);
    boolean resultValue =  iReaderPictureContentService.insert(readerPictureContent);
     if(!resultValue){
           ResultMessageInfo.ofMessage("添加失败！");
     }
     return  ResultMessageInfo.ofSuccess("添加成功！");
 }

/**
 * @function 修改
 * @param readerPictureContent 实体对象
 * @return
 */
@PostMapping("update")
@Transactional
public ResultMessageInfo update(@RequestBody  ReaderPictureContent readerPictureContent) {
    ValidatorUtil.validateEntity(readerPictureContent, AddGroup.class);//非空验证
    Integer userId = this.getUserId();
    readerPictureContent.setUpdtDt(new Date());
    readerPictureContent.setUpdtUserId(userId);
    boolean resultValue =  iReaderPictureContentService.updateById(readerPictureContent);
    if (!resultValue) {
        return ResultMessageInfo.ofMessage("修改失败！");
    }
    return ResultMessageInfo.ofSuccess("修改成功！");
}

/**
 * @function 单条删除
 * @param  readerPictureContent
 * @return
 */
 @PostMapping("delete")
 @Transactional
 public ResultMessageInfo deleteById(@RequestBody ReaderPictureContent readerPictureContent){
    Integer userId = this.getUserId();
    readerPictureContent.setDelFlag(1);
    readerPictureContent.setUpdtDt(new Date());
    readerPictureContent.setUpdtUserId(userId);
    boolean resultValue =  iReaderPictureContentService.deleteById(readerPictureContent.getId());
    if (!resultValue) {
        ResultMessageInfo.ofMessage("删除失败！");
    }
    return ResultMessageInfo.ofSuccess("删除成功！");

 }


/**
 * @function 批量删除对象
 * @param data 键为ids值为整型数组的json格式
 * @return
 */
 @PostMapping("deleteIds")
 @Transactional
 public ResultMessageInfo deleteBatchIds(@RequestBody String data){
    List<Long> ids = JSON.parseArray( JSON.parseObject(data).getString("ids"),Long.class);
    boolean resultValue =  iReaderPictureContentService.deleteBatchIds(ids);
    if (!resultValue) {
        ResultMessageInfo.ofMessage("删除失败！");
    }
    return ResultMessageInfo.ofSuccess("删除成功！");
 }
}